package annotations;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by feng on 16/7/12.
 * to parse the annotations of {@link MemberBean}
 */
public class TableCreator {

    public static void main(String[] a) {
       /* if (args.length < 1) {
            System.out.println("arguments: annotated classes");
            System.exit(0);
        }*/

        String[] args = new String[]{"annotations.MemberBean"};

        for (String className : args) {
            try {
                Class<?> cl = Class.forName(className);
                DBTable dbTable = cl.getAnnotation(DBTable.class);
                if (null == dbTable) {
                    System.out.println("[No DbTable annotations in class " + cl + "]");
                    continue;
                }

                String tableName = dbTable.name();

                if (tableName.length() < 1) {
                    tableName = cl.getName().toUpperCase();
                }

                List<String> columnDefs = new ArrayList<>();

                for (Field field : cl.getDeclaredFields()) {
                    String column = null;
                    Annotation[] annotations = field.getDeclaredAnnotations();

                    if (annotations.length < 1) {
                        continue;
                    }

                    if (annotations[0] instanceof SQLInteger) {
                        SQLInteger sqlInteger = (SQLInteger) annotations[0];
                        if (sqlInteger.name().length() < 1) {
                            //use field name if column name not specified
                            column = field.getName().toUpperCase();
                        } else {
                            column = sqlInteger.name();
                        }

                        columnDefs.add(column + " INT" + getConstraint(sqlInteger.constraint()));
                    } else if (annotations[0] instanceof SQLString) {
                        SQLString sqlString = (SQLString) annotations[0];
                        if (sqlString.name().length() < 1) {
                            column = field.getName().toUpperCase();
                        } else {
                            column = sqlString.name();
                        }

                        columnDefs.add(column + " VARCHAR(" + sqlString.value() + ")" + getConstraint(sqlString.constraint()));
                    }

                }

                StringBuffer createcommand = new StringBuffer("CREATE TABLE " + tableName + "(");
                for (String columnDef : columnDefs) {
                    createcommand.append("\n    " + columnDef + ",");
                }

                String tableCreate = createcommand.subSequence(0, createcommand.length() - 1) + ")";

                System.out.println("Table Creation SQL FOR  [" + className + "] is : \n" + tableCreate );


            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }


    private static String getConstraint(Constraints constraints) {
        String ct = "";
        if (constraints != null) {
            if (!constraints.allowNull()) {
                ct += " NOT NULL";
            }

            if (constraints.primaryKey()) {
                ct += " PRIMARY KEY";
            }

            if (constraints.unique()) {
                ct += " UNIQUE";
            }

            return ct;

        }
        return "";
    }
}
